/**
 * NoteDao.java
 * com.ebook.server.dao
 * Copyright (c) 2013, 公司版权所有.
*/

package com.ebook.server.dao;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.ebook.core.domain.Note;
import com.ebook.util.DateUtil;

/**
 * 
 * @author    潘吉友
 * @Date	 2013-7-25 	 
 */
@Repository
public class NoteDao {
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	private static Log logger = LogFactory.getLog(NoteDao.class);
	
	/**
	 * 插入笔记数据
	 * @param note 要插入的笔记信息
	 * @param uid
	 * @param updateTimeFlag true:创建新的更新时间；false：用笔记信息内的更新时间
	 * @param syncTime 笔记同步的时间
	*/
	public boolean insertExists(Note note, int uid,boolean updateTimeFlag,long syncTime) {
		String insertSql = "insert into note (noteid,bookid,uid,create_time,update_time,upload_time,curr_chapter,"
				+ "select_content,note_content,issynronezed,contain_file,startCFI,endCFI,note_index,isNote,color,isExist,chapter_title)"
				+ " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		logger.info("|insertExists|"+insertSql);
		try {
			jdbcTemplate.update(
					insertSql,
					new Object[] { note.getNoteid(), note.getBookid(), uid, note.getCreate_time(),
							updateTimeFlag ? DateUtil.dateToLong(new Date()):note.getUpdate_time(),syncTime,
							note.getCurr_chapter(), note.getSelect_content(),note.getNote_content(), 1, note.getContain_file(),
							note.getStartCFI(), note.getEndCFI(),note.getNote_index(), note.getIsNote(), note.getColor(),
							note.getIsExist(), note.getChapter_title() });
			return true;
		} catch (Exception e) {
			logger.error("|insertExists|insert note error!");
			throw new RuntimeException(e);
		}
	}
	
	public boolean insertNotExists(Note note,int uid,long syncTime) {
		String insertSql = "insert into note (noteid,bookid,uid,create_time,update_time,upload_time,curr_chapter,"
				+ "select_content,note_content,issynronezed,contain_file,startCFI,endCFI,note_index,isNote,color,isExist,chapter_title)"
				+ " values (?,?,?,11112233445566777,11112233445566777,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		logger.info("|insertNotExists|"+insertSql);
		try {
			jdbcTemplate.update(
					insertSql,
					new Object[] { note.getNoteid(), note.getBookid(), uid,syncTime,
							note.getCurr_chapter(), "","", 1, note.getContain_file(),
							"", "",note.getNote_index(), note.getIsNote(), "",0, "" });
			return true;
		} catch (Exception e) {
			logger.error("|insertNotExists|insert note error!");
			throw new RuntimeException(e);
		}
	}
	
	/**
	 *更新笔记，设置upload_time
	 * @param note
	 * @param uid
	 * @param syncTime
	 * @return boolean
	*/
	public boolean updateExists(Note note,int uid,long syncTime){
		String sql = "update note set bookid=?,uid=?,create_time=?,update_time=?,upload_time=?,curr_chapter=?,"
				+ "select_content=?,note_content=?,issynronezed=?,contain_file=?,startCFI=?,endCFI=?,note_index=?," 
				+ "isNote=?,color=?,isExist=?,chapter_title=? where noteid=?";
		logger.info("|updateExists|"+sql);
		try{
			jdbcTemplate.update(sql, note.getBookid(),uid,note.getCreate_time(),note.getUpdate_time(),syncTime,note.getCurr_chapter(),
					note.getSelect_content(),note.getNote_content(),note.getIssynronezed(),note.getContain_file(),
					note.getStartCFI(),note.getEndCFI(),note.getNote_index(),note.getIsNote(),note.getColor(),note.getIsExist(),
					note.getChapter_title(),note.getNoteid());
			return true;
		}catch(Exception e){
			logger.error("|updateExists|insert note error!");
			throw new RuntimeException(e);
		}		
	}
	
	public boolean updateNotExists(String noteId,long syncTime){
		String updateSql = "update note set create_time=11112233445566777,update_time=11112233445566777,upload_time=?,select_content=\"\","
			+ "note_content=\"\",startCFI=\"\",endCFI=\"\",color=\"\",isExist=?,chapter_title=\"\" where noteid=?";
		try{
			logger.info("|updateNotExists|"+updateSql);
			jdbcTemplate.update(updateSql,syncTime, 0,noteId);
			return true;
		}catch(Exception e){
			logger.error("|updateNotExists|insert note error!");
			throw new RuntimeException(e);
		}
	}
	
	public boolean isNoteExist(Note note) {
		String sql = "select count(*) from note where noteid=?";
		if (jdbcTemplate.queryForInt(sql, note.getNoteid()) > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	public int countNoteByUidAndBookid(int uid, int bookid) {
		String sql = "select count(*) from note where uid=? and bookid=?";
		logger.info(sql);
		return jdbcTemplate.queryForInt(sql, uid,bookid);
	}
	
	public boolean deleteByUploadTime(int uid,int bookid,long lastSyncTime){
		String sql = "delete from note where uid=? and bookid=? and upload_time<=? and isExist=0";
		logger.info(sql);
		jdbcTemplate.update(sql, uid,bookid,lastSyncTime);
		return true;
	}
	
	public Map<String,Object> queryByNoteid(String noteId){
		String sql = "select * from note where noteid=?";
		try {
			return jdbcTemplate.queryForMap(sql, noteId);			
		} catch (Exception e) {
			return null;
		}
	}

}

